﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using SoftwareConsulting.BI11.Studio.Core.Project.SituationsHierarchy;
using SoftwareConsulting.BI11.Studio.InternalServices;
using SoftwareConsulting.BI11.Studio.UI.Properties;

namespace SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.SituationsHierarchy
{
    /// <summary>
    /// Диалог редактирования вероятностей перехода по характеристикам "следующий шаг" и "шаг при неудаче"
    /// </summary>
    public partial class ProbabilityDialog : RestorableForm
    {
        private Characteristic _characteristic;//редактируемая характеристика

        /// <summary>
        /// Отображение на графике текущих значений
        /// </summary>
        private void Initialize()
        {
            GraphPane myPane = ProbGraph.GraphPane;
            myPane.Legend.IsVisible = false;
            myPane.IsIgnoreMissing = false;
            myPane.Title.IsVisible = false;
            myPane.XAxis.Title.Text = Resources.BI_PROBABILITY_CHARACTERISTICS_CAPTION;
            myPane.YAxis.Title.Text = Resources.BI_PROBABILITY_PROBABILITY_CAPTION;
            myPane.XAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;
            myPane.YAxis.Title.FontSpec.FontColor = Color.Red;
            myPane.YAxis.Scale.Align = AlignP.Inside;
            myPane.YAxis.Scale.Max = 100;
            myPane.YAxis.Scale.Min = 0;
            myPane.XAxis.Type = AxisType.Text;
            myPane.XAxis.Scale.Min = 0;
            myPane.XAxis.Scale.MinorStep = 1;
            myPane.XAxis.Scale.MajorStep = 1;
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f);
            ProbGraph.IsShowHScrollBar = true;
            ProbGraph.IsShowVScrollBar = true;
            ProbGraph.IsAutoScrollRange = true;
            ProbGraph.IsShowPointValues = true;
            myPane.CurveList.Clear();
            PointPairList graphLine = new PointPairList();
            Situation sit = _characteristic.OwningSituation;
            int i=-1;
            List<string> characteristicnames=new List<string>();
            foreach(Characteristic ch in sit.WalkCharacteristicsByIndicatorTypes(_characteristic.IndicatorType))
            {
                i++;                
                double val = ch.TurnProbability * 100;
                graphLine.Add(0, val,Convert.ToInt32(Math.Round(val)).ToString(CultureInfo.CurrentCulture));                
                characteristicnames.Add(ch.Name);
            }
            myPane.XAxis.Scale.TextLabels = characteristicnames.ToArray();            
            myPane.AddBar("", graphLine, GeneralMethods.GetColorByIndex(0));                        
            ProbGraph.AxisChange();
            ProbGraph.Invalidate();
        }

        /// <summary>
        /// Конструктор формы
        /// </summary>
        /// <param name="characteristic"></param>
        public ProbabilityDialog(Characteristic characteristic)
        {
            _characteristic = characteristic;
            InitializeComponent();
        }

        private void ProbabilityDialog_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        private string ProbGraph_PointEditEvent(ZedGraphControl sender, GraphPane pane, CurveItem curve, int iPt)
        {
            PointPair pt = curve[iPt];
            double val = pt.Y;
            int col = iPt;
            Situation sit = _characteristic.OwningSituation;
            int i=-1;
            foreach (Characteristic ch in sit.WalkCharacteristicsByIndicatorTypes(_characteristic.IndicatorType))
            {
                i++;
                if (i == col)
                {                    
                    ch.TurnProbability = val / 100;
                    break;
                }
            }
            Initialize();
            return val.ToString(CultureInfo.CurrentCulture);
        }
    }
}
